package cn.ldw.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Cloth {
    void produce();
    int comsume();
}

class Nike implements Cloth {

    @Override
    public void produce() {
        System.out.println("开始生产衣服");
    }

    @Override
    public int comsume() {
        System.out.println("买衣服");
        return 1;
    }
}

class ProFactory {
    public static Object getProxyObject(Object obj) {
        InvocationHandler handle = new MyInvocationHandler(obj);
        Object proxyInstance = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handle);
        return proxyInstance;
    }
}

class MyInvocationHandler implements InvocationHandler {
    private Object obj;

    public MyInvocationHandler(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("制作中");
        Object invoke = method.invoke(obj);
        System.out.println("生产中");
        return invoke;
    }
}

public class Model {
    public static void main(String[] args) {
        Nike nike = new Nike();
        Cloth proxyObject = (Cloth)ProFactory.getProxyObject(nike);
        int comsume = proxyObject.comsume();
        System.out.println(comsume);
        proxyObject.produce();
    }
}
